<?php
namespace Admin\Service;

/**
 * 本系统自己的html生成类，其他模板请重写此类
 */
class HtmlService
{
    /**
     * 角色select
     * @param  string $select_id [description]
     * @return [type]            [description]
     */
    public function groupSelect($select_id = '')
    {
        $data = M('admin_group')->getField('id,title', true);
        return $this->buildSelect($data, 'group_id', $select_id);
    }

    public function categorySelect($categroy_id = '')
    {
        $data = M('category')
            ->where([
                'parent_id' => 0,
            ])
            ->getField('id,name', true);
        return $this->buildSelect($data, 'category_id', $categroy_id, true);
    }

    /**
     * example中select
     * @param  string $select_id [description]
     * @return [type]            [description]
     */
    public function exampleSelect($select_id = '')
    {
        $data = [1, 2, 3, 4];
        return $this->buildSelect($data, 'e_select', $select_id);
    }

    /**
     * example中的radio
     * @param  string $checked_id [description]
     * @return [type]             [description]
     */
    public function exampleRadio($checked_id = '')
    {
        $data = [
            1 => '男',
            2 => '女',
            3 => '保密',
        ];
        return $this->buildRadio($data, 'e_select', $checked_id);
    }

    /**
     * 教材版本
     * @param  string $version_id [description]
     * @return [type]             [description]
     */
    public function versionSelect($version_id = '')
    {
        $data = M('version')->getField('id,title', true);
        return $this->buildSelect($data, 'version_id', $version_id, "width:120px;");
    }

    /**
     * 教材版本
     * @param  string $version_id [description]
     * @return [type]             [description]
     */
    public function unitSelect($book_id, $unit_id)
    {
        $data = M('unit')->where(['book_id' => $book_id])->getField('id,title', true);
        return $this->buildSelect($data, 'unit_id', $unit_id, "width:220px;");
    }

    /**
     * 生成select
     * @param  [type] $arr         [description]
     * @param  [type] $id          [description]
     * @param  string $selected_id [description]
     * @return [type]              [description]
     */
    protected function buildSelect($arr, $id, $selected_id = '', $empty = false)
    {
        $html = "<span class='select-box'><select id='" . $id . "' name='" . $id . "' class='select'>";

        if ($empty) {
            $html .= '<option value="0">--请选择--</option>';
        }

        foreach ($arr as $key => $val) {
            $html .= '<option value="' . $key . '"';
            if (isset($selected_id) && $selected_id == $key) {
                $html .= " selected=\"selected\" ";
            }
            $html .= '>' . $val . '</option>';
        }
        $html .= '</select></span>';

        return $html;
    }

    protected function buildRadio($arr, $id, $checked_id = '')
    {
        $html = '';

        foreach ($arr as $key => $value) {
            $html .= "<div class='radio-box'>
                        <input type='radio' id='" . $id . "' name='" . $id . "' value='" . $key . "' ";
            if (isset($checked_id) && $checked_id == $key) {
                $html .= " checked";
            }
            $html .= "><label for='" . $id . "'>" . $value . "</label>
                    </div>";
        }

        return $html;
    }
}
